package com.lywz.biz.service.city.impl;

import java.util.Collection;
import java.util.Collections;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lywz.biz.common.dao.CommonDao;
import com.lywz.biz.common.entity.city.CityEntertainment;
import com.lywz.biz.common.entity.city.CityInfo;
import com.lywz.biz.common.entity.city.CityTourismOrganization;
import com.lywz.biz.common.entity.city.CityTraffic;
import com.lywz.biz.common.enums.City;
import com.lywz.biz.dao.city.CityDao;
import com.lywz.biz.service.city.CityInfoService;

@Service("cityInfoService")
public class CityInfoServiceImpl implements CityInfoService {

    @Autowired
    private CityDao cityDao;

    @Autowired
    private CommonDao commonDao;

    @Override
    public Collection<CityInfo> getCityInfo() {
        Collection<CityInfo> collection;
        try {
            collection = cityDao.getOverview();
        } catch (Exception e) {
            e.printStackTrace();
            collection = Collections.emptyList();
        }
        return collection;
    }

    @Override
    public Collection<CityTourismOrganization> getInsideOrg(Short cityId) {
        Collection<CityTourismOrganization> collection;
        try {
            collection = cityDao.getInsideOrgByCityId(cityId);
        } catch (Exception e) {
            e.printStackTrace();
            collection = Collections.emptyList();
        }
        return collection;
    }

    @Override
    public CityInfo getOneCityById(Short id) {
        CityInfo city = new CityInfo();
        try {
            city = (CityInfo) commonDao.queryById(CityInfo.class, id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return city;
    }

    @Override
    public Collection<CityTourismOrganization> getOutsideOrg(Short cityId) {
        Collection<CityTourismOrganization> collection;
        try {
            collection = cityDao.getOutsideOrgByCityId(cityId);
        } catch (Exception e) {
            e.printStackTrace();
            collection = Collections.emptyList();
        }
        return collection;
    }

    @Override
    public CityTraffic getTrafficById(Short cityId) {
        CityTraffic traffic = new CityTraffic();
        try {
            traffic = cityDao.getOneCityTrafficByCityId(cityId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return traffic;
    }

    @Override
    public boolean addCityBasicInfo(CityInfo cityInfo) {
        try {
            commonDao.saveOrUpdate(cityInfo);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean modifyCityBasicInfo(CityInfo cityInfo) {
        try {
            commonDao.update(cityInfo);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 首先找到已经添加到数据库中的城市信息的cityId， 在City枚举中，将这些已经存在的city删除，并返回剩余部分
     */
    public Collection<City> getUnaddedCityFromCityBasic() {
        //存储要返回的city列表
        Collection<City> cityListTemp = City.getCityList();
        //已经添加到数据库中的city信息列表
        Collection<CityInfo> cityInfoList;
        try {
            cityInfoList = cityDao.getOverview();
        } catch (Exception e) {
            e.printStackTrace();
            return Collections.emptyList();
        }
        for (CityInfo cityInfo : cityInfoList) {
            int cityId = cityInfo.getCityId();
            City cityTemp = City.valueOfById(cityId);
            if (cityTemp != null) cityListTemp.remove(cityTemp);
        }
        return cityListTemp;
    }

    /************************** 城市交通 ******************************************/
    @Override
    public Collection<CityTraffic> getCityTrafficList() {
        Collection<CityTraffic> cityTrafficList;
        try {
            cityTrafficList = cityDao.getCityTrafficList();
        } catch (Exception e) {
            e.printStackTrace();
            cityTrafficList = Collections.emptyList();
        }
        return cityTrafficList;
    }

    @Override
    public boolean addCityTraffic(CityTraffic cityTraffic) {
        try {
            commonDao.saveOrUpdate(cityTraffic);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public CityTraffic getCityTrafficById(Short id) {
        CityTraffic traffic = new CityTraffic();
        try {
            traffic = commonDao.queryById(CityTraffic.class, id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return traffic;
    }

    @Override
    public boolean modifyCityTraffic(CityTraffic cityTraffic) {
        try {
            commonDao.update(cityTraffic);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 首先找到已经添加到数据库中的城市信息的cityId， 在City枚举中，将这些已经存在的city删除，并返回剩余部分
     */
    public Collection<City> getUnaddedCityFromTraffic() {
        //存储要返回的city列表
        Collection<City> cityListTemp = City.getCityList();
        //已经添加到数据库中的city信息列表
        Collection<CityTraffic> cityTrafficList;
        try {
            cityTrafficList = cityDao.getCityTrafficList();
        } catch (Exception e) {
            e.printStackTrace();
            return Collections.emptyList();
        }
        for (CityTraffic cityTraffic : cityTrafficList) {
            int cityId = cityTraffic.getCityId();
            City cityTemp = City.valueOfById(cityId);
            if (cityTemp != null) cityListTemp.remove(cityTemp);
        }
        return cityListTemp;
    }

    /**************************
     * 城市旅游机构
     * 
     * @throws Exception *
     *************************************************************/
    @Override
    public Collection<CityTourismOrganization> getCityTourismOrgListByCityId(Short cityId,
            boolean insideOrNot) {
        Collection<CityTourismOrganization> cityTourismOrg;
        try {
            if (insideOrNot == true) {
                cityTourismOrg = cityDao.getInsideOrgByCityId(cityId);
            } else {
                cityTourismOrg = cityDao.getOutsideOrgByCityId(cityId);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Collections.emptyList();
        }
        return cityTourismOrg;
    }
    @Override
    public boolean addTourismOrg(CityTourismOrganization tourismOrg)
    {
        try {
            commonDao.saveOrUpdate(tourismOrg);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    @Override
    public boolean deleteTourismOrgById(Integer id)
    {
        try {
            commonDao.delete(commonDao.queryById(CityTourismOrganization.class, id));
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    @Override
    public boolean modifyTourismOrg(CityTourismOrganization tourismOrg)
    {
        try {
            commonDao.update(tourismOrg);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    /***********************************  都市娱乐   ***************************************/

    @Override
    public CityEntertainment getEntertainmentByCityId(Short cityId) {
        try {
            return cityDao.getCityEntertainmentByCityId(cityId);
        } catch (Exception e) {
            e.printStackTrace();
            return new CityEntertainment();
        }
    }
    @Override
    public Collection<CityEntertainment> getEntertainmentList()
    {
        try {
            return cityDao.getCityEntertainmentList();
        } catch (Exception e) {
            e.printStackTrace();
            return Collections.emptyList();
        }
    }
    @Override
    public CityEntertainment getEntertainmentById(int id){
        try {
          return  commonDao.queryById(CityEntertainment.class, id);
        } catch (Exception e) {
            e.printStackTrace();
            return new CityEntertainment();
        }
    }
    @Override
    public boolean modifyEntertainment(CityEntertainment cityEntertainment)
    {
        try {
            commonDao.update(cityEntertainment);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    /**
     * 首先找到已经添加到数据库中的城市信息的cityId， 在City枚举中，将这些已经存在的city删除，并返回剩余部分
     */
    public Collection<City> getUnaddedCityFromEntertainment() {
        //存储要返回的city列表
        Collection<City> cityListTemp = City.getCityList();
        //已经添加到数据库中的city信息列表
        Collection<CityEntertainment> cityEntertainmentList;
        try {
            cityEntertainmentList = cityDao.getCityEntertainmentList();
        } catch (Exception e) {
            e.printStackTrace();
            return Collections.emptyList();
        }
        for (CityEntertainment entertainment : cityEntertainmentList) {
            int cityId = entertainment.getCityId();
            City cityTemp = City.valueOfById(cityId);
            if (cityTemp != null) cityListTemp.remove(cityTemp);
        }
        return cityListTemp;
    }   
    /*********************************** 通用 **************************************************/

    /**
     * 首先找到已经添加到数据库中的城市信息的cityId， 在City枚举中，将这些已经存在的city删除，并返回剩余部分
     * 通过参数判断，调用getUnaddedCityFromCityBasic 或者 getUnaddedCityFromTraffic
     */
    @Override
    public Collection<City> getUnaddedCity(String flag) {
        if (flag.equals("basic")) {
            return getUnaddedCityFromCityBasic();
        } else if(flag.equals("taffic")) {
            return getUnaddedCityFromTraffic();
        }else if(flag.equals("entertainment")){
            return getUnaddedCityFromEntertainment();
        }else{
            return Collections.emptyList();
        }
    }

    @Override
    public boolean addEntertainment(CityEntertainment entertainment)
    {
        try {
            commonDao.saveOrUpdate(entertainment);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    /****************************** setter getter ****************************************/
    public void setCityDao(CityDao cityDao) {
        this.cityDao = cityDao;
    }

    @Override
    public CityTraffic getCityTrafficByCityId(Short cityId) {
        CityTraffic traffic = new CityTraffic();
        try {
            traffic = cityDao.getOneCityTrafficByCityId(cityId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return traffic;
    }


}
